﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using CommonLibrary.DB;
using CommonLibrary.DataModel.Basic;
using System.Data;

namespace ServiceApplication.Dao.Basic
{
    public class FileDao
    {
        public int AddEntity(FileEntity fileEntity)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@file_id", fileEntity.file_id));
            parameterList.Add(new SqlParameter("@file_name", fileEntity.file_name));
            parameterList.Add(new SqlParameter("@file_replacename", fileEntity.file_replacename));
            parameterList.Add(new SqlParameter("@file_path", fileEntity.file_path));
            parameterList.Add(new SqlParameter("@file_ctime", fileEntity.file_ctime));
            parameterList.Add(new SqlParameter("@file_size", fileEntity.file_size));
            parameterList.Add(new SqlParameter("@user_id", fileEntity.user_id));
            parameterList.Add(new SqlParameter("@file_mime", fileEntity.file_mime));
            parameterList.Add(new SqlParameter("@file_descn", fileEntity.file_descn));
            parameterList.Add(new SqlParameter("@file_ext", fileEntity.file_ext));
            parameterList.Add(new SqlParameter("@file_catalog", fileEntity.file_catalog));
            parameterList.Add(new SqlParameter("@file_share", fileEntity.file_share));
            parameterList.Add(new SqlParameter("@file_utime", fileEntity.file_utime));
            parameterList.Add(new SqlParameter("@folder_id", fileEntity.folderID));


            string sql = "insert into user_file (file_name,file_replacename,file_path,file_ctime,file_size,user_id,file_mime, file_descn,file_ext,file_catalog,file_share,file_utime,folder_id) values " +
                " (@file_name,@file_replacename,@file_path,@file_ctime,@file_size,@user_id,@file_mime,@file_descn,@file_ext,@file_catalog,@file_share,@file_utime,@folder_id); " +
                " select @@identity";
            string dept_id = DBExecuteUtil.getUniqueResult(sql, parameterList.ToArray());
            return int.Parse(dept_id);
        }

        /// <summary>
        ///  删除文件
        /// </summary>
        /// <param name="file_id"></param>
        public void DeleteEntity(int file_id)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@file_id", file_id));
            string sql = "delete from user_file where file_id=@file_id ";
            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }



        //public FileEntity QueryEntity(string file_replacename)
        //{
        //    FileEntity fileEntity = null;
        //    List<SqlParameter> parameterList = new List<SqlParameter>();
        //    parameterList.Add(new SqlParameter("@file_replacename", file_replacename));
        //    string sql = "select file_id,file_name,file_replacename,file_path,file_ctime,file_size,user_id,file_mime, file_descn,file_ext,file_catalog,file_share,file_utime,folder_id " +
        //           " from user_file where file_replacename=@file_replacename";

        //    var result = DBExecuteUtil.querySqlArray(sql, parameterList.ToArray());
        //    if (result == null) return null;

        //    fileEntity = new FileEntity();
        //    fileEntity.file_id = int.Parse(result[0][0]);
        //    fileEntity.file_name = result[0][1];
        //    fileEntity.file_replacename = result[0][2];
        //    fileEntity.file_path = result[0][3];
        //    fileEntity.file_ctime = result[0][4];
        //    fileEntity.file_size = result[0][5];
        //    fileEntity.user_id = int.Parse(result[0][6]);
        //    fileEntity.file_mime = result[0][7];
        //    fileEntity.file_descn = result[0][8];
        //    fileEntity.file_ext = result[0][9];
        //    fileEntity.file_catalog = result[0][10];
        //    fileEntity.file_share = result[0][11];
        //    fileEntity.file_utime = result[0][12];
        //    fileEntity.folderID = result[0][13];
        //    return fileEntity;
        //}

        public List<FileEntity> QueryEntities(String user_id)
        {
            List<FileEntity> fileEntities = new List<FileEntity>();
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@user_id", user_id));
            string sql = "select file_id,file_name,file_replacename,file_path,file_ctime,file_size,user_id,file_mime, file_descn,file_ext,file_catalog,file_share,file_utime,folder_id " +
                   " from user_file where user_id = @user_id";

            var result = DBExecuteUtil.querySqlArray(sql, parameterList.ToArray());
            if (result == null) return fileEntities;
            for (int index = 0; index < result.Length; index++)
            {
                var fileEntity = new FileEntity();
                fileEntity.file_id = int.Parse(result[index][0]);
                fileEntity.file_name = result[index][1];
                fileEntity.file_replacename = result[index][2];
                fileEntity.file_path = result[index][3];
                fileEntity.file_ctime = result[index][4];
                fileEntity.file_size = result[0][5];
                fileEntity.user_id = int.Parse(result[index][6]);
                fileEntity.file_mime = result[index][7];
                fileEntity.file_descn = result[index][8];
                fileEntity.file_ext = result[index][9];
                fileEntity.file_catalog = result[index][10];
                fileEntity.file_share = result[index][11];
                fileEntity.file_utime = result[0][12];
                fileEntity.folderID = result[0][13];

                fileEntities.Add(fileEntity);
            }

            return fileEntities;
        }

        public List<FileEntity> QueryEntitiesByFolderId(String folder_id)
        {
            List<FileEntity> fileEntities = new List<FileEntity>();
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@folder_id", folder_id));
            string sql = "select file_id,file_name,file_replacename,file_path,file_ctime,file_size,user_id,file_mime, file_descn,file_ext,file_catalog,file_share,file_utime,folder_id " +
                   " from user_file where folder_id = @folder_id";

            var result = DBExecuteUtil.querySqlArray(sql, parameterList.ToArray());
            if (result == null) return fileEntities;
            for (int index = 0; index < result.Length; index++)
            {
                var fileEntity = new FileEntity();
                fileEntity.file_id = int.Parse(result[index][0]);
                fileEntity.file_name = result[index][1];
                fileEntity.file_replacename = result[index][2];
                fileEntity.file_path = result[index][3];
                fileEntity.file_ctime = result[index][4];
                fileEntity.file_size = result[0][5];
                fileEntity.user_id = int.Parse(result[index][6]);
                fileEntity.file_mime = result[index][7];
                fileEntity.file_descn = result[index][8];
                fileEntity.file_ext = result[index][9];
                fileEntity.file_catalog = result[index][10];
                fileEntity.file_share = result[index][11];
                fileEntity.file_utime = result[0][12];
                fileEntity.folderID = result[0][13];

                fileEntities.Add(fileEntity);
            }

            return fileEntities;
        }

        public List<FileEntity> QueryEntities(String file_catalog, String userId)
        {
            List<FileEntity> fileEntities = new List<FileEntity>();
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@file_catalog", file_catalog));
            parameterList.Add(new SqlParameter("@user_id", userId));
            string sql = "select file_id,file_name,file_replacename,file_path,file_ctime,file_size,user_id,file_mime, file_descn,file_ext,file_catalog,file_share,file_utime,folder_id " +
                   " from user_file where file_catalog=@file_catalog and user_id = @user_id";

            var result = DBExecuteUtil.querySqlArray(sql, parameterList.ToArray());
            if (result == null) return fileEntities;
            for (int index = 0; index < result.Length; index++)
            {
                var fileEntity = new FileEntity();
                fileEntity.file_id = int.Parse(result[index][0]);
                fileEntity.file_name = result[index][1];
                fileEntity.file_replacename = result[index][2];
                fileEntity.file_path = result[index][3];
                fileEntity.file_ctime = result[index][4];
                fileEntity.file_size = result[0][5];
                fileEntity.user_id = int.Parse(result[index][6]);
                fileEntity.file_mime = result[index][7];
                fileEntity.file_descn = result[index][8];
                fileEntity.file_ext = result[index][9];
                fileEntity.file_catalog = result[index][10];
                fileEntity.file_share = result[index][11];
                fileEntity.file_utime = result[0][12];
                fileEntity.folderID = result[0][13];

                fileEntities.Add(fileEntity);
            }

            return fileEntities;
        }

        public List<FileEntity> QueryShareEntities()
        {
            List<FileEntity> fileEntities = new List<FileEntity>();
            string sql = "select file_id,file_name,file_replacename,file_path,file_ctime,file_size,user_id,file_mime, file_descn,file_ext,file_catalog,file_share,file_utime,folder_id " +
                   " from user_file where file_share='true' ";

            var result = DBExecuteUtil.querySqlArray(sql);
            if (result == null) return fileEntities;
            for (int index = 0; index < result.Length; index++)
            {
                var fileEntity = new FileEntity();
                fileEntity.file_id = int.Parse(result[index][0]);
                fileEntity.file_name = result[index][1];
                fileEntity.file_replacename = result[index][2];
                fileEntity.file_path = result[index][3];
                fileEntity.file_ctime = result[index][4];
                fileEntity.file_size = result[0][5];
                fileEntity.user_id = int.Parse(result[index][6]);
                fileEntity.file_mime = result[index][7];
                fileEntity.file_descn = result[index][8];
                fileEntity.file_ext = result[index][9];
                fileEntity.file_catalog = result[index][10];
                fileEntity.file_share = result[index][11];
                fileEntity.file_utime = result[0][12];
                fileEntity.folderID = result[0][13];

                fileEntities.Add(fileEntity);
            }

            return fileEntities;
        }

        public void UpdateEntity(FileEntity fileEntity)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@file_id", fileEntity.file_id));
            parameterList.Add(new SqlParameter("@file_name", fileEntity.file_name));
            parameterList.Add(new SqlParameter("@file_replacename", fileEntity.file_replacename));
            parameterList.Add(new SqlParameter("@file_path", fileEntity.file_path));
            parameterList.Add(new SqlParameter("@file_ctime", fileEntity.file_ctime));
            parameterList.Add(new SqlParameter("@file_size", fileEntity.file_size));
            parameterList.Add(new SqlParameter("@user_id", fileEntity.user_id));
            parameterList.Add(new SqlParameter("@file_mime", fileEntity.file_mime));
            parameterList.Add(new SqlParameter("@file_descn", fileEntity.file_descn));
            parameterList.Add(new SqlParameter("@file_ext", fileEntity.file_ext));
            parameterList.Add(new SqlParameter("@file_catalog", fileEntity.file_catalog));
            parameterList.Add(new SqlParameter("@file_share", fileEntity.file_share));

            string sql = "update user_file set file_name=@file_name,file_replacename=@file_replacename,file_path=@file_path,file_ctime=@file_ctime," +
                " file_size=@file_size,user_id=@user_id,file_mime=@file_mime, file_descn=@file_descn,file_ext=@file_ext,file_catalog=@file_catalog, file_share=@file_share where file_id=@file_id";

            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }

        //public void MoveFile(String file_id, String file_catalog)
        //{
        //    List<SqlParameter> parameterList = new List<SqlParameter>();
        //    parameterList.Add(new SqlParameter("@file_id", file_id));
        //    parameterList.Add(new SqlParameter("@file_catalog", file_catalog));
        //    string sql = "update user_file set file_catalog=@file_catalog where file_id=@file_id";
        //    DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        //}





        /// <summary>
        ///  获得文件表格
        /// </summary>
        /// <param name="fileID"></param>
        /// <returns></returns>
        public DataTable GetFile(String fileID)
        {
            String sql = "SELECT [file_id],[file_name],[file_replacename],[file_path],[file_ctime],[file_utime],[file_size]," +
                "[user_id],[file_mime],[file_descn],[file_ext],[file_share],[folder_id] FROM user_file WHERE file_id=@file_id";
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@file_id", fileID));
            return DBExecuteUtil.querySqlTable(sql, parameterList.ToArray());
        }

        /// <summary>
        /// 根据用户id获取对应所有文件信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetFileByUserId(int user_id)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@user_id", user_id));

            String sql = "SELECT file_id 文件编码,file_name 文件名称,file_replacename 虚拟名称,file_path 文件路径,file_ctime 创建时间,file_utime 更新时间,file_size 文件大小," +
                   "user_id 用户编码,file_mime 文件类型,file_descn 描述,file_ext 文件后缀,file_share 共享标志,folder_id 文件夹编码 FROM user_file WHERE user_id=@user_id";

            return DBExecuteUtil.querySqlTable(sql, parameterList.ToArray());
        }

        public DataTable GetFileAllShared()
        {
            String sql = "SELECT file_id 文件编码,file_name 文件名称,file_replacename 虚拟名称,file_path 文件路径,file_ctime 创建时间,file_utime 更新时间,file_size 文件大小," +
                   "f.user_id 用户编码,user_name 用户名称,file_mime 文件类型,file_descn 描述,file_ext 文件后缀,file_share 共享标志,folder_id 文件夹编码 FROM user_file f,jc_user u WHERE f.user_id=u.user_id and file_share!='' ";

            return DBExecuteUtil.querySqlTable(sql);
        }

        public DataTable GetFileSharedByUserId(string user_id)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@user_id", user_id));
            String sql = "SELECT file_id 文件编码,file_name 文件名称,file_replacename 虚拟名称,file_path 文件路径,file_ctime 创建时间,file_utime 更新时间,file_size 文件大小," +
                   "f.user_id 用户编码,user_name 用户名称,file_mime 文件类型,file_descn 描述,file_ext 文件后缀,file_share 共享标志,folder_id 文件夹编码 FROM user_file f,jc_user u WHERE f.user_id=u.user_id and file_share!='' and f.user_id=@user_id ";

            return DBExecuteUtil.querySqlTable(sql,parameterList.ToArray());
        }
        /// <summary>
        ///  获得文件实体
        /// </summary>
        /// <param name="file_id"></param>
        /// <returns></returns>
        public FileEntity QueryEntity(String file_id)
        {
            FileEntity fileEntity = null;
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@file_id", file_id));
            string sql = "select file_id,file_name,file_replacename,file_path,file_ctime,file_size,user_id,file_mime, file_descn,file_ext,file_catalog,file_share,file_utime,folder_id " +
                " from user_file where file_id=@file_id";

            var result = DBExecuteUtil.querySqlArray(sql, parameterList.ToArray());
            if (result == null) return null;

            fileEntity = new FileEntity();
            fileEntity.file_id = int.Parse(result[0][0]);
            fileEntity.file_name = result[0][1];
            fileEntity.file_replacename = result[0][2];
            fileEntity.file_path = result[0][3];
            fileEntity.file_ctime = result[0][4];
            fileEntity.file_size = result[0][5];
            fileEntity.user_id = int.Parse(result[0][6]);
            fileEntity.file_mime = result[0][7];
            fileEntity.file_descn = result[0][8];
            fileEntity.file_ext = result[0][9];
            fileEntity.file_catalog = result[0][10];
            fileEntity.file_share = result[0][11];
            fileEntity.file_utime = result[0][12];
            fileEntity.folderID = result[0][13];
            return fileEntity;
        }
        /// <summary>
        ///  删除文件
        /// </summary>
        /// <param name="fileID"></param>
        /// <param name="baseUrl"></param>
        public void DeleteFile(String fileID, String baseUrl)
        {
            var file = this.GetFile(fileID);
            this.deleteFile(fileID);
            if (!String.IsNullOrEmpty(baseUrl))
                System.IO.File.Delete(System.IO.Path.Combine(baseUrl, file.Rows[0]["file_replacename"].ToString() + file.Rows[0]["file_ext"].ToString()));
        }

        private void deleteFile(String fileID)
        {
            String sql = "DELETE FROM user_file WHERE [file_id]=@fileID";
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@fileID", fileID));
            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }

        /// <summary>
        ///  获得文件表格
        /// </summary>
        /// <param name="folderID"></param>
        /// <returns></returns>
        public DataTable GetFiles(String folderID)
        {
            String sql = "SELECT [file_id],[file_name],[file_replacename],[file_path],[file_ctime],[file_size],[user_id],[file_mime]," +
                 " [file_descn],[file_ext],[file_catalog],[file_share],[file_utime],[folder_id] from user_file where [folder_id]=@folderID";
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@folderID", folderID));

            return DBExecuteUtil.querySqlTable(sql, parameterList.ToArray());
        }

        /// <summary>
        ///  获得文件列表
        /// </summary>
        /// <param name="folderID"></param>
        /// <returns></returns>
        public List<FileEntity> GetFileList(String folderID, String userID)
        {
            String sql = "SELECT [file_id],[file_name],[file_replacename],[file_path],[file_ctime],[file_size],[user_id],[file_mime], [file_descn]," +
                    "[file_ext],[file_catalog],[file_share],[file_utime],[folder_id] FROM user_file WHERE [folder_id]=@folderID AND [user_id]=@userID";
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@folderID", folderID));
            parameterList.Add(new SqlParameter("@userID", userID));

            var result = DBExecuteUtil.querySqlTable(sql, parameterList.ToArray());

            List<FileEntity> fileList = new List<FileEntity>();
            foreach (DataRow item in result.Rows)
            {
                fileList.Add(new FileEntity
                {
                    file_id = Int32.Parse(item["file_id"].ToString()),
                    file_name = item["file_name"].ToString(),
                    file_replacename = item["file_replacename"].ToString(),
                    file_path = item["file_path"].ToString(),
                    file_ctime = item["file_ctime"].ToString(),
                    file_size = item["file_size"].ToString(),
                    user_id = Int32.Parse(item["user_id"].ToString()),
                    file_mime = item["file_mime"].ToString(),
                    file_descn = item["file_descn"].ToString(),
                    file_ext = item["file_ext"].ToString(),
                    file_catalog = item["file_catalog"].ToString(),
                    file_share = item["file_share"].ToString(),
                    file_utime = item["file_utime"].ToString(),
                    folderID = item["folder_id"].ToString()
                });
            }
            return fileList;
        }

        #region 模糊查询

        /// <summary>
        /// mlh 用于根据文件名模糊查询文件夹
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="userID"></param>
        /// <returns></returns>
        public List<FileEntity> getFiles4Fuzzy(String fileName, String userID)
        {
            fileName = "%" + fileName + "%";
            String sql = "select [file_id],[file_name],[file_replacename],[file_path],[file_ctime],[file_size],[user_id],[file_mime]," +
                    " [file_descn],[file_ext],[file_catalog],[file_share],[file_utime],[folder_id] " +
                    " FROM user_file WHERE [file_name] like @fileName AND [user_id]=@user_id";

            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@fileName", fileName));
            parameterList.Add(new SqlParameter("@user_id", userID));
            var result = DBExecuteUtil.querySqlTable(sql, parameterList.ToArray());

            List<FileEntity> filelist = new List<FileEntity>();
            foreach (DataRow item in result.Rows)
            {
                filelist.Add(new FileEntity
                {
                    file_id = int.Parse(item["file_id"].ToString()),
                    file_name = item["file_name"].ToString(),
                    file_replacename = item["file_replacename"].ToString(),
                    file_path = item["file_path"].ToString(),
                    file_ctime = item["file_ctime"].ToString(),
                    file_size = item["file_size"].ToString(),
                    user_id = int.Parse(item["user_id"].ToString()),
                    file_mime = item["file_mime"].ToString(),
                    file_descn = item["file_descn"].ToString(),
                    file_ext = item["file_ext"].ToString(),
                    file_catalog = item["file_catalog"].ToString(),
                    file_share = item["file_share"].ToString(),
                    file_utime = item["file_utime"].ToString(),
                    folderID = item["folder_id"].ToString()
                });
            }
            return filelist;
        }
        public List<FolderEntity> getSharedFiles4Fuzzy(string fileName, string userID)
        {
            return null;
        }

        #endregion
        /// <summary>
        ///  删除所有文件
        /// </summary>
        /// <param name="files"></param>
        public void DeleteFiles(DataTable files, String baseUrl)
        {
            foreach (DataRow item in files.Rows)
            {
                this.DeleteFile(item["file_id"].ToString(), baseUrl);
            }
        }

        /// <summary>
        ///  获得有共享员工的ID
        /// </summary>
        /// <returns></returns>
        public String[][] GetShareUserID()
        {
            String sql = "SELECT DISTINCT [user_id],[file_share] FROM user_file WHERE [file_share]!=''";
            return DBExecuteUtil.querySqlArray(sql);
        }

        /// <summary>
        ///  获得所有员工有网盘的ID
        /// </summary>
        /// <returns></returns>
        public String[][] GetAllFiledUserID()
        {
            String sql = "SELECT DISTINCT [user_id] FROM user_file";
            return DBExecuteUtil.querySqlArray(sql);
        }

        /// <summary>
        ///  获得共享文件列表
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public List<FileEntity> GetShareFileList(String userID)
        {
            String sql = "SELECT [file_id],[file_name],[file_replacename],[file_path],[file_ctime],[file_size],[user_id],[file_mime], [file_descn]," +
                     "[file_ext],[file_catalog],[file_share],[file_utime],[folder_id] FROM user_file WHERE [user_id]=@user_id AND [file_share]!=''";
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@user_id", userID));

            var result = DBExecuteUtil.querySqlTable(sql, parameterList.ToArray());

            List<FileEntity> fileList = new List<FileEntity>();
            foreach (DataRow item in result.Rows)
            {
                fileList.Add(new FileEntity
                {
                    file_id = Int32.Parse(item["file_id"].ToString()),
                    file_name = item["file_name"].ToString(),
                    file_replacename = item["file_replacename"].ToString(),
                    file_path = item["file_path"].ToString(),
                    file_ctime = item["file_ctime"].ToString(),
                    file_size = item["file_size"].ToString(),
                    user_id = Int32.Parse(item["user_id"].ToString()),
                    file_mime = item["file_mime"].ToString(),
                    file_descn = item["file_descn"].ToString(),
                    file_ext = item["file_ext"].ToString(),
                    file_catalog = item["file_catalog"].ToString(),
                    file_share = item["file_share"].ToString(),
                    file_utime = item["file_utime"].ToString(),
                    folderID = item["folder_id"].ToString()
                });
            }
            return fileList;
        }

        /// <summary>
        ///  获得用户所有文件
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public List<FileEntity> GetUserFiles(String userID)
        {
            String sql = "SELECT [file_id],[file_name],[file_replacename],[file_path],[file_ctime],[file_size],[user_id],[file_mime], [file_descn]," +
                    "[file_ext],[file_catalog],[file_share],[file_utime],[folder_id] FROM user_file WHERE [user_id]=@user_id";
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@user_id", userID));

            var result = DBExecuteUtil.querySqlTable(sql, parameterList.ToArray());

            List<FileEntity> fileList = new List<FileEntity>();
            foreach (DataRow item in result.Rows)
            {
                fileList.Add(new FileEntity
                {
                    file_id = Int32.Parse(item["file_id"].ToString()),
                    file_name = item["file_name"].ToString(),
                    file_replacename = item["file_replacename"].ToString(),
                    file_path = item["file_path"].ToString(),
                    file_ctime = item["file_ctime"].ToString(),
                    file_size = item["file_size"].ToString(),
                    user_id = Int32.Parse(item["user_id"].ToString()),
                    file_mime = item["file_mime"].ToString(),
                    file_descn = item["file_descn"].ToString(),
                    file_ext = item["file_ext"].ToString(),
                    file_catalog = item["file_catalog"].ToString(),
                    file_share = item["file_share"].ToString(),
                    file_utime = item["file_utime"].ToString(),
                    folderID = item["folder_id"].ToString()
                });
            }
            return fileList;
        }

        /// <summary>
        ///  分享文件
        /// </summary>
        /// <param name="fileID"></param>
        public void ShareFile(String file_id, String share_flag)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@file_id", file_id));
            parameterList.Add(new SqlParameter("@file_share", share_flag));
            string sql = "update user_file set file_share=@file_share where file_id=@file_id";
            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }

        public void ShareFileByFolderID(String folder_id, String isShare)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@folder_id", folder_id));
            parameterList.Add(new SqlParameter("@file_share", isShare));
            string sql = "update user_file set file_share=@file_share where folder_id=@folder_id";
            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }

        /// <summary>
        ///  移动文件
        /// </summary>
        /// <param name="fileID"></param>
        /// <param name="folderID"></param>
        public void MoveFile(String fileID, String folderID)
        {
            String sql = "UPDATE user_file SET [folder_id]=@folderID WHERE [file_id]=@fileID ";
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@fileID", fileID));
            parameterList.Add(new SqlParameter("@folderID", folderID));
            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }

        /// <summary>
        ///  重命名文件
        /// </summary>
        /// <param name="fileID"></param>
        /// <param name="rename"></param>
        public void RenameFile(String fileID, String rename)
        {
            String sql = "UPDATE user_file SET [file_name]=@fileName WHERE [file_id]=@fileID";
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@fileName", rename));
            parameterList.Add(new SqlParameter("@fileID", fileID));
            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }
    }
}
